package Servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import db.DBUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

public class GetReplyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String reply = "";
        String author = "";
        String id = request.getParameter("id");

        try {
            Connection connect = DBUtil.getConnect();
            Statement statement = connect.createStatement(); // Statement可以理解为数据库操作实例，对数据库的所有操作都通过它来实现

            String sql = "select author,reply from replys where id = '"+id+"'";
            ResultSet result = statement.executeQuery(sql);

            while(result.next()){
                author = result.getString("author");
                reply = result.getString("reply");

                HashMap<String, String> map = new HashMap<>();
                map.put("author",author);
                map.put("reply",reply);

                Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
                response.setContentType("text/html;charset=utf-8"); // 设置响应报文的编码格式
                PrintWriter pw = response.getWriter(); // 获取 response 的输出流
                pw.println(gson.toJson(map)); // 通过输出流把业务逻辑的结果输出
                pw.flush();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        doGet(request, response);
    }
}
